// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

// 1 绑定原型
import axios from "axios";

// 绑定到原型
Vue.prototype.$axios = axios;

//设置默认的 api 域名
axios.defaults.baseURL = "http://127.0.0.1:3000"

//2 引入ui库

// 引入 ui 库
import ElementUI from 'element-ui';
// 引入 css
import 'element-ui/lib/theme-chalk/index.css';
//注册组件库
Vue.use(ElementUI);

// 3.设置路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path !='/'){
    const token =localStorage.getItem('token');
    if(!token){
      router.push('/');
    }else{
      next()
    }
  }else{
    next()
  }
})

// 4.请求拦截器
axios.interceptors.request.use(config=>{
  if(!config.headers.Authorization&&localStorage.getItem('token')){
      config.headers.Authorization=localStorage.getItem('token')
  }
  return config
})
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})



// 设置默认样式图片路径
Vue.prototype.$url=function(url){
  var res= '';
  if(url.indexOf('http')<0){
    res=axios.defaults.baseURL+url
  }else{
    res=url
  }
  return res
}